Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

add AdapterManager in to Zend\Db\Adapter namespace #2903

Closed

Conversation

ClemensSahs
Copy link
Contributor

this AdapterManger allows handle multible database connections and aliase

unit tests will follow

#autoload/global.php
return array(
    'db_adapter_manager'=>array(
        'user'=>'global',

        'global'=>array(
            'driver'=>array(
                'driver'=>'Pdo_Mysql',
                'database'=>'my_database_name',
                'username'=>'root',
                'password'=>null,
                'characterset'=>'utf8'
            )
        ),
    ),
);
# module.php
    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'db_adapter_manager' => 'Zend\Db\Adapter\AdapterServiceFactory',
                'my_user_db_adapter' => function ($sm) {
                    $dbAdapterContainer = $sm->get('db_adapter_manager');
                    return $dbAdapterContainer->getAdapter('user');
                },
            )
        );
    }
# controller
    public function fooAction()
    {
         /* @var $dam\Zend\Mvc\Service\DbAdapterManager */
         $dam = $this->getServiceLocator()->get('db_adapter_manager');

         if ( $dam->hasAdapterConfig('global') ) {
             /* @var $adapter \Zend\Db\Adapter\Adapter */
             $adapter = $dam->getAdapter('global');
             $dam->addAdapter('news',$adapter);
         } else {
             // do something else
         }
    }
    public function userFooAction()
    {
         /* @var $userAdapter \Zend\Db\Adapter\Adapter */
         $userAdapter= $this->getServiceLocator()->get('my_user_db_adapter');
         $result = $userAdapter->query('SQL Querys',$userAdapter::QUERY_MODE_EXECUTE);
         var_dump ( $result->current );
    }

@ralphschindler
Copy link
Member

I think this all belongs in the Zend\Mvc\Service location.

@ClemensSahs
Copy link
Contributor Author

@ralphschindler
yes this is a better place for "AdapterManagerFactory" ... you think move both?

@ralphschindler
Copy link
Member

Yes, I think both should be moved, they are only useful in an MVC application that consumes the ServiceManager (as described since both of these have dependencies on the ServiceManager).

* @throws InvalidArgumentException
* @return Adapter
*/
public function factoryAdapter($config, ServiceLocatorInterface $serviceLocator=null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to "adapterFactory"

@weierophinney
Copy link
Member

Two additional notes:

  • Needs unit tests! :)
  • Also, Zend\Db\Adapter\AdapterServiceFactory already uses the "db" key, which means that we could end up with a configuration conflict. I think the top-level configuration key for your AdapterManager needs to be renamed -- perhaps something like "db_adapter_manager" or "db_multi".

@ClemensSahs
Copy link
Contributor Author

after the proposal from ralph i tried to solve the dependencies to the ServiceManager package...
The tow last commits of this branch are one Version.
Currently i only have this case in for databases. Theoretically this comes for every class you want init smartly...
So I'm not sure what way is the right:

  • do it only for DB ( with the current proposal )
  • do it with a abstract way
  • or do nothing and everyone do it self ( but ugly )

@weierophinney
thanks i will patch this ;)

@tihe
Copy link

tihe commented Dec 22, 2012

Is this available for end users (configuring/using multiple databases)? If not could you please point me in the right direction for an intermediate solution?

@weierophinney
Copy link
Member

@ClemensSahs do you have time to do this in the next 1-2 weeks? If not, I need to change the milestone.

@zensys This is a pull request adding the feature; it's not available currently in the master repository.

@ralphschindler
Copy link
Member

Do you have any additional comments here? If not, do you mind if I take this for inclusion in 2.1, my only change is that it resides in the Zend\Mvc\Service namespace as it has dependencies on Zend\Mvc conventions and also promotes a configuration convention.

@ClemensSahs
Copy link
Contributor Author

@weierophinney
currently i have time agin, sry for be so far away

@ralphschindler yes i will change some think and will implement unit tests

for all
i will try this changes to this Weekend

- patch some coding standards
- rename config key from 'db' to 'db_adapter_manager'
@weierophinney
Copy link
Member

@ClemensSahs Feature freeze is in a couple days. I'm going to remove the milestone. If you get it done by then, I'll re-assign it to 2.1.

ClemensSahs and others added 7 commits January 22, 2013 14:45
- fix typo
- change the expected Exception for "testWrongAlias"
- fix issue find by "testAdapterDuplicatKey"
- fix typo
- clean tailing spaces
- fix typo
@ClemensSahs
Copy link
Contributor Author

@weierophinney
I think I'm finished. if no one has if objections or improvements.

@zensys
no it is finished, I have update the description. So its easy to write a Doc

@radnan
Copy link
Contributor

radnan commented Jan 23, 2013

I'm wondering if DbAdapterManager can just extend off of Zend\ServiceManager\AbstractPluginManager. That way you don't have to recreate so much of the service locator functions. The DbAdapterManagerFactory can then just be an extension of Zend\Mvc\Service\AbstractPluginManagerFactory.

The DbAdapterManager should be added to the list of configurable service managers in Zend\Mvc\Service\ModuleManagerFactory. This way users can manage the db adapters just as they would any other service/plugin provider.

@ClemensSahs
Copy link
Contributor Author

To implement the ServiceLocator has only one benefits. Add the ability to use ServiceKeys like aliase.
So if you have a "driver factory" you can use it, the same with a platform object.

I think a extention of AbstractPluginManager to much for this case.
Or I misunderstand your point?

@radnan
Copy link
Contributor

radnan commented Jan 24, 2013

Right, but extending the AbstractPluginManager gives you all that for free essentially. This way you can:

  • add factories to define custom adapters
    • look up config and other stuff from within the factory
  • use the built-in aliasing
  • use invokables for custom drivers and platforms

and not have to reinvent a lot of the stuff you're doing in your manager.

@ClemensSahs
Copy link
Contributor Author

Sounds nice but overstocked. The main benefit of my PR is a multi db alternative for Zend\Db\Adapter\AdapterServiceFactory if your improvements make it easy for End-User, then I'm vote for it too.

But I see currently only more complexity. Can you give me a example config for your version like the one in the description?

@radnan
Copy link
Contributor

radnan commented Jan 24, 2013

Sure:

class Module
{
    public function getConfig()
    {
        return array(
            'db_adapters' => array(
                'adapters' => array(
                    'module1' => array(
                        'driver' => 'pdo_mysql',
                        'username' => 'user',
                        'password' => 'pass',
                    ),
                ),
                'aliases' => array(
                    'default' => 'module1',
                ),
                'factories' => array(
                    'module2' => function ()
                    {
                        return new Adapter($driver, $platform);
                    },
                ),
            ),
        );
    }
}

class IndexController
{
    /** @var \Zend\Db\AdapterManager */
    protected $adapters;

    public function indexAction()
    {
        $adapter = $this->adapters->get('default');
    }
}

@ClemensSahs
Copy link
Contributor Author

@radnan
here is a version with implement your improvements. Its a preview, currently I want move the building methods in to a separate factoring Class.
ClemensSahs@bcd7815

refactoring in progress for Unit-Test and Exceptions

PS: the methodes I mean

  • getQueryResultPrototype
  • getDriverObjectFromConfig
  • getPlatformObjectFromConfig
  • createFromAdapterConfig

any feedback?

@ralphschindler
Copy link
Member

@ClemensSahs I find your patch pretty interesting.
Since @EvanDotPro wrote the originaly factory in Zend\Db, I'd like to get his opinion on this too, also since he had a hand in ZfcUser, which consumes database adapters.

* @var Adapter[]
*/
protected $_dbAdapter = array();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't prefix non-public members with underscores; no longer needed with ZF2 CS.

@radnan
Copy link
Contributor

radnan commented Jan 26, 2013

I'd like to humbly submit my take on the issue:

radnan@42245d4

@ClemensSahs
Copy link
Contributor Author

@radnan
its looks nice ;)

but on this way we don't can customizes "plattform objects" and "result prototype"
Currently I miss this is a feature on your commit...

@ralphschindler
Copy link
Member

I am going to close this PR. The idea is good, but this particular implementation is not what needs to happen. Probably @radnan's implementation is closer, if he were to move it into the Zend\Mvc namespace. From there I think we can open an new PR when that one is ready.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants